package handlers

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"xiao-hong-shu-app/models"
	"xiao-hong-shu-app/pkg"
)

func Login(c *gin.Context) {
	tel := c.PostForm("tel")
	password := c.PostForm("password")
	var user models.User
	err := models.DB.Where("tel=?", tel).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号不存在",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	token := pkg.TokenHandler(strconv.Itoa(int(user.ID)))
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "登陆成功",
		"token": token,
	})
	return

}
